package com.itheima.zookeeper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

import com.itheima.mode_test.User;

/**
 * @author ：yuan_he_song
 */
public class StreamTest {

        @Test // forEach 循环
        public void test1(){
            List<String> list = Arrays.asList("you", "don't", "bird", "me", "I", "don't", "bird", "you");
            list.stream().forEach(s -> System.out.println(s));
        }
        @Test //filter 过滤
        public void test2(){
            List<User> users = Arrays.asList(new User(15),
                    new User(28), new User(20), new User(30), new User(18));
            List<User> list = users.stream().filter(user -> user.getId() > 18).collect(Collectors.toList());
            System.out.println(list);
        }
        @Test
        public void map(){
            List<String> list = Arrays.asList("how", "are", "you", "how", "old", "are", "you", "?");
            List<String> collect = list.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());
            System.out.println(collect);
            System.out.println(list);
        }
        @Test
        public void flatMap(){
            List<Integer> a = Arrays.asList(1, 2, 3);
            List<Integer> b = Arrays.asList(4, 5, 6);
            List<Integer> collect = Stream.of(a, b).flatMap(list -> list.stream()).collect(Collectors.toList());
            System.out.println(collect);
        }
        @Test
        public void sort(){
            List<Integer> list = Arrays.asList(4, 5, 6);
            Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(k -> k, k -> k * 10));
            System.out.println(collect);
        }
        @Test
        public void distinct(){
            // 知之为知之,不知为不知
            List<String> list = Arrays.asList("aaa", "ccc", "bbb", "aaa", "bbb", "ddd", "eee", "eee");
            Set<String> collect = list.stream().collect(Collectors.toSet());
            System.out.println(collect);
        }
        @Test
        public void join(){
            List<User> users = Arrays.asList(new User(1, "張三"), new User(2, "李四"), new User(3, "王五"));
            Map<Integer, String> map = users.stream().collect(Collectors.toMap(User::getId, User::getName));
            System.out.println(map);
            String name = map.get(2);
            System.out.println(name);
        }

        @Test
        public void concat(){
            List<String> list = Arrays.asList("a", "b", "c");
            String result = list.stream().collect(Collectors.joining(","));
            System.out.println(result);
        }

    }
